﻿using AutoMapper;

namespace Maltose.ToExtensions
{
    /// <summary>
    /// AutoMapper扩展
    /// </summary>
    public static class AutoMapperExtensions
    {
        /// <summary>
        /// 自动赋值
        /// </summary>
        /// <typeparam name="TDestination">转换后的类型</typeparam>
        /// <typeparam name="TSource">转换前的类型</typeparam>
        /// <param name="source">需要转换类型的值</param>
        /// <returns></returns>
        public static TDestination ToMap<TDestination, TSource>(this TSource source)
       where TDestination : class
       where TSource : class
        {
            if (source == null) return default(TDestination);
            var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());
            var mapper = config.CreateMapper();
            return mapper.Map<TDestination>(source);
        }
    }
}
